﻿using FastTests;
using Raven.Server.Documents.Queries.AST;
using Raven.Server.Documents.Queries.Parser;
using Tests.Infrastructure;
using Xunit;
using Xunit.Abstractions;

namespace SlowTests.Issues
{
    public class RavenDB_18674 : RavenTestBase
    {
        public RavenDB_18674(ITestOutputHelper output) : base(output)
        {
        }

        [RavenFact(RavenTestCategory.Querying)]
        public void WrongQueryExpressionWhenDeclaringFunction()
        {
            var parser = new QueryParser();
            parser.Init("DECLARE function output(o) { return { Total: o.Total }; }"+
                            "from index 'Orders/ByCompany' as o " +
                            "where Total > 1000 select output(o)");
            var query = parser.Parse(QueryType.Select);
            var s = query.ToString();
            Assert.False(s.Contains("function output function output(o)"), "duplicate of \'function output\' in query.ToString()");

        }

        private class Item
        {
            public string Id { get; set; }
            public string Content { get; set; }
        }

    }

}
